<script setup lang="ts">
import useLoginStore from '@/store/login/login'
import { mapRouteToMenu } from '@/utils/map-menus'
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'

// 获取动态菜单
const loginStore = useLoginStore()
const userMenu = loginStore.userMenu
// console.log(userMenu)
// 折叠侧边栏
defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
})
// 监听item得点击
const router = useRouter()
const handleRouterPath = (item: any) => {
  // console.log(item.url)
  router.push(item.url)
}
// elmenu的默认菜单
// 第一次进入 为第一个路由
const route = useRoute()
const defaultActive = computed(() => {
  const nowPath = mapRouteToMenu(userMenu, route.path)
  return nowPath.id + ''
})
</script>
<template>
  <div class="main-aside">
    <div class="main-aside-top">
      <img class="logo" src="@/assets/img/logo.svg" alt="" />
      <h2 v-show="!isCollapse" class="title">后台管理系统</h2>
    </div>
    <div class="main-aside-menu">
      <el-menu
        text-color="#b7bdc3"
        active-text-color="#fff"
        background-color="#001529"
        :unique-opened="true"
        :default-active="defaultActive"
        :collapse="isCollapse"
      >
        <template v-for="item in userMenu" :key="item.id">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <!-- 字符串专为组件 icon:"el-icon-monitor" -->
              <el-icon>
                <component :is="item.icon.split('-icon-')[1]"></component>
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <template v-if="item.children.length > 0">
              <template v-for="child in item.children" :key="child.id">
                <el-menu-item
                  :index="child.id + ''"
                  @click="handleRouterPath(child)"
                >
                  {{ child.name }}
                </el-menu-item>
              </template>
            </template>
          </el-sub-menu>
        </template>
      </el-menu>
    </div>
  </div>
</template>
<style scoped lang="less">
.main-aside {
  .main-aside-top {
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    .logo {
      width: 28px;
      height: 28px;
    }
    .title {
      color: #fff;
      font-size: 20px;
    }
  }
  .main-aside-menu {
  }
  .el-menu {
    border-right: none;
    user-select: none;
  }
  .el-sub-menu {
    .el-menu-item {
      padding-left: 50px;
      background-color: #0c2135;
    }
    .el-menu-item:hover,
    .el-sub-menu__title:hover {
      color: #fff;
      background-color: #0a60bd;
    }
    .el-menu-item.is-active {
      background-color: #0a60bd;
    }
  }
}
</style>
